/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Session;

import Cart.Cart;
import DAO.DAO;
import DTO.Address;
import DTO.Customer;
import DTO.Item;
import DTO.Order;
import DTO.Payment;
import DTO.ShippingMethod;
import Exception.EmptySet;
import Exception.KwickKioskDBException;
import com.sun.imageio.plugins.common.I18N;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tejinder
 */
public  class SessionManager {

    protected Customer customer ;
    protected int cutomerId;
    protected boolean loggedIn;
    protected Cart cart;
    protected DAO dao;
    protected List<Item> searchItemsInSession  = null;
    protected boolean onPathToPurchase;
    protected Item singleItemOnPathToPurchase;
    protected List<Item> cartItemsInSession = null;
    protected Address shipaddressForOrder;
    protected ShippingMethod shipMethodForOrder;
    protected double shipCostOrder = 0.0;
    protected ArrayList<Payment> paymentList;
    protected Payment orderPymtDet;
    protected Order order;
    protected List<Item> listDiscountCart = null;
    protected Double cartSubTotal = 0.00;
    private Double cartTotal = 0.00;
    private Double orderTotal = 0.00;
    private Double orderTax = 0.00;
    private Double totalDiscount = 0.00;
    public SessionManager(DAO dao){
        this.dao = dao;
        customer = new Customer();
        loggedIn = false;
        cart = new Cart(dao, this);
        searchItemsInSession = new ArrayList<Item>();
        cartItemsInSession = new ArrayList<Item>();
        singleItemOnPathToPurchase = new Item();
        shipaddressForOrder = new Address();
        shipMethodForOrder = new ShippingMethod();
        orderPymtDet = new Payment();
        listDiscountCart  = new ArrayList<Item>();
        paymentList = new ArrayList<Payment>();
    }

    public SessionManager(Customer cust, boolean logIn){
        customer = cust;
        loggedIn = logIn;
    }

    //GETTERS AND SETTERS ------------------------------------------------------

    public int getCustomerId(){
        return customer.getStoreCardNumber();
    }
    public Cart getCart() {
        return cart;
    }

    public  Customer getCustomer() {
        return customer;
    }

    /**
     * @param aCustomerId the customerId to set
     */
    public boolean setCustomer(String storeCard) {
        try {

            customer = dao.getCustomer(Integer.parseInt(storeCard));
            if (customer == null){
                return false;
            }
        } catch (EmptySet ex) {
            System.out.println("card not found");
            return false;
        } catch (KwickKioskDBException kkdb) {
            System.out.println("database down"); //NEED TO IMPLEMENT THIS FOR REAL
            return false;
        }
        
        loggedIn = true;
        cart.calculateTotals();
        return true;
    }

    /**
     * @return the loggedIn
     */
    public boolean getLoggedIn() {
        return loggedIn;
    }

    /**
     * @param loggedIn the loggedIn to set
     */
    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    /**
     * @return the cutomerId
     */
    public int getCutomerId() {
        return cutomerId;
    }

    /**
     * @param cutomerId the cutomerId to set
     */
    public void setCutomerId(int cutomerId) {
        this.cutomerId = cutomerId;
    }

    /**
     * @return the searchItemsInSession
     */
    public List<Item> getSearchItemsInSession() {
        return searchItemsInSession;
    }

    /**
     * @param searchItemsInSession the searchItemsInSession to set
     */
    public void setSearchItemsInSession(List<Item> itemsInSession) {
        this.searchItemsInSession = itemsInSession;
    }

        /**
     * @return the onPathToPurchase
     */
    public boolean isOnPathToPurchase() {
        return onPathToPurchase;
    }

    /**
     * @param onPathToPurchase the onPathToPurchase to set
     */
    public void setOnPathToPurchase(boolean onPathToPurchase) {
        this.onPathToPurchase = onPathToPurchase;
    }

    /**
     * @return the singleItemOnPathToPurchase
     */
    public Item getSingleItemOnPathToPurchase() {
        return singleItemOnPathToPurchase;
    }

    /**
     * @param singleItemOnPathToPurchase the singleItemOnPathToPurchase to set
     */
    public void setSingleItemOnPathToPurchase(Item singleItemOnPathToPurchase) {
        this.singleItemOnPathToPurchase = singleItemOnPathToPurchase;
    }

    /**
     * @return the cartItemsInSession
     */
    public List<Item> getCartItemsInSession() {
        return cartItemsInSession;
    }

    /**
     * @param cartItemsInSession the cartItemsInSession to set
     */
    public void setCartItemsInSession(List<Item> cartItemsInSession) {
        this.cartItemsInSession = cartItemsInSession;
    }

    /**
     * @return the shipaddressForOrder
     */
    public Address getShipaddressForOrder() {
        return shipaddressForOrder;
    }

    /**
     * @param shipaddressForOrder the shipaddressForOrder to set
     */
    public void setShipaddressForOrder(Address shipaddressForOrder) {
        this.shipaddressForOrder = shipaddressForOrder;
    }


    /**
     * @return the shipMethodForOrder
     */
    public ShippingMethod getShipMethodForOrder() {
        return shipMethodForOrder;
    }

    /**
     * @param shipMethodForOrder the shipMethodForOrder to set
     */
    public void setShipMethodForOrder(ShippingMethod shipMethodForOrder) {
        this.shipMethodForOrder = shipMethodForOrder;
    }

    /**
     * @return the shipCostOrder
     */
    public double getShipCostOrder() {
        return shipCostOrder;
    }

    /**
     * @param shipCostOrder the shipCostOrder to set
     */
    public void setShipCostOrder(double shipCostOrder) {
        this.shipCostOrder = shipCostOrder;
    }

    /**
     * @return the orderPymtDet
     */
    public Payment getOrderPymtDet() {
        return orderPymtDet;
    }

    /**
     * @param orderPymtDet the orderPymtDet to set
     */
    public void setOrderPymtDet(Payment orderPymtDet) {
        this.orderPymtDet = orderPymtDet;
    }

    /**
     * @return the order
     */
    public Order getOrder() {
        return order;
    }

    /**
     * @param order the order to set
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * @return the listDiscountCart
     */
    public List<Item> getListDiscountCart() {
        return listDiscountCart;
    }

    /**
     * @param listDiscountCart the listDiscountCart to set
     */
    public void setListDiscountCart(List<Item> listDiscountCart) {
        this.listDiscountCart = listDiscountCart;
    }

    /**
     * @return the cartSubTotal
     */
    public Double getCartSubTotal() {
        return cartSubTotal;
    }

    /**
     * @param cartSubTotal the cartSubTotal to set
     */
    public void setCartSubTotal(Double cartSubTotal) {
        this.cartSubTotal = cartSubTotal;
    }

    public ArrayList<Payment> getPaymentList() {
        return paymentList;
    }

    // FUNCTIONALITY------------------------------------------------------------

    public void searchByUPC(String upc){
        cart.searchByUPC(upc);
    }

    public void searchByCategory(int ctid){
        cart.searchByCategory(ctid);
    }

    public void addItemToCart(Item item, int qty){
        cart.addItem(item, qty);
    }

    public void logout(){
        customer = new Customer();
        loggedIn = false;
        cart.calculateTotals();
    }

    public void addPayment(Payment pymnt){
        paymentList.add(pymnt);
    }

    public boolean enoughPayment(){
        if (totalDue()<= 0.0)
            return true;
        else
            return false;
    }

    public double totalPaid(){
        double hasPaid = 0.0;
        for(Payment pay : paymentList){
            hasPaid += pay.getAmount();
        }
        return hasPaid;
    }

    public double totalDue(){
        
        
        DecimalFormat formatter = new DecimalFormat(".00");
        BigDecimal bigD = new BigDecimal(formatter.format(cart.getTotal()-totalPaid()));
        System.out.println(bigD.doubleValue());
        return (bigD.doubleValue());
    }

    public void fullfillOrder(){
        String date = new Date().toString();
        order = new Order(cart.getSubtotal(), cart.getTaxAmount(),
                    cart.getTotalDiscount(), cart.getOrderDiscount(),
                    cart.getStoreCardDiscount(), "WebStore", date, cart.getItemsInCart(), paymentList);
        try {
            dao.makeStoreOrder(order);
        } catch (KwickKioskDBException ex) {
            Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        cart = new Cart(dao, this);
        paymentList = new ArrayList<Payment>();
    }

    /**
     * @return the cartTotal
     */
    public Double getCartTotal() {
        return cartTotal;
    }

    /**
     * @param cartTotal the cartTotal to set
     */
    public void setCartTotal(Double cartTotal) {
        this.cartTotal = cartTotal;
    }

    /**
     * @return the orderTotal
     */
    public Double getOrderTotal() {
        return orderTotal;
    }

    /**
     * @param orderTotal the orderTotal to set
     */
    public void setOrderTotal(Double orderTotal) {
        this.orderTotal = orderTotal;
    }

    /**
     * @return the orderTax
     */
    public Double getOrderTax() {
        return orderTax;
    }

    /**
     * @param orderTax the orderTax to set
     */
    public void setOrderTax(Double orderTax) {
        this.orderTax = orderTax;
    }

    /**
     * @return the totalDiscount
     */
    public Double getOrderDiscount() {
        return getTotalDiscount();
    }

    /**
     * @param totalDiscount the totalDiscount to set
     */
    public void setOrderDiscount(Double orderDiscount) {
        this.setTotalDiscount(orderDiscount);
    }

    public void clearSession(){
         setLoggedIn(false);
        //clearing session
        setCartItemsInSession(null);
        setCartSubTotal(0.00);
        setCartTotal(0.00);
        setOnPathToPurchase(false);
        setListDiscountCart(null);
        setOrderDiscount(0.00);
        setOrderTax(0.00);
        setOrderTotal(0.00);
        setShipCostOrder(0.00);
        setListDiscountCart(null);
        setOrderPymtDet(null);
        setCutomerId(0);


    }

    /**
     * @return the totalDiscount
     */
    public Double getTotalDiscount() {
        return totalDiscount;
    }

    /**
     * @param totalDiscount the totalDiscount to set
     */
    public void setTotalDiscount(Double totalDiscount) {
        this.totalDiscount = totalDiscount;
    }
}
